
package com.android.matmanual;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class ScreenDeadPointTest extends BaseActivity 
{
	WakeLock mWakeLock;
	ImageView mScreenDeadPoint;
	ColorChangeHandler mColorChangeHandler;
	private static final int CHANGE_GREEN = 1;  
	private static final int CHANGE_WHITE = 2; 
	private static final int CHANGE_BLACK = 3; 
	private static final int CHANGE_RED = 4; 
	private static final int CHANGE_FINISH = 5; 
	private boolean mFinishFlag = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    	        WindowManager.LayoutParams.FLAG_FULLSCREEN);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.screendeadpoint_test);
		 PowerManager pm =
		           (PowerManager) getSystemService(POWER_SERVICE); 
		    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, 
		          "XYTEST"); 
		    mWakeLock.acquire(); 
				
		mScreenDeadPoint = (ImageView) findViewById(R.id.screen_dead_point);
		setBuutonVisiable(false);
		mColorChangeHandler = new ColorChangeHandler();
		mColorChangeHandler.sendEmptyMessageDelayed(CHANGE_GREEN, 1000);
	}
	

    private class ColorChangeHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			  switch (msg.what) {
              case CHANGE_GREEN: 
            	  mColorChangeHandler.sendEmptyMessageDelayed(CHANGE_WHITE, 1000);            	  
            	  mScreenDeadPoint.setBackgroundColor(0xff00ff00);
            	  break;
              case CHANGE_WHITE: 
            	  mColorChangeHandler.sendEmptyMessageDelayed(CHANGE_BLACK, 1000);
            	  mScreenDeadPoint.setBackgroundColor(0xffffffff); 
            	  break;
              case CHANGE_BLACK: 
            	  mColorChangeHandler.sendEmptyMessageDelayed(CHANGE_RED, 1000);
            	  mScreenDeadPoint.setBackgroundColor(0xff000000); 
            	  break;
              case CHANGE_RED: 
            	  mColorChangeHandler.sendEmptyMessageDelayed(CHANGE_FINISH, 1000);
            	  mScreenDeadPoint.setBackgroundColor(0xffff0000);           
            	  break;
              case CHANGE_FINISH: 
            	  if (TestControl.IsAuto()) {
            		  mFinishFlag = true;
            		  mScreenDeadPoint.setVisibility(View.INVISIBLE);
            	  } else {
            		  setBuutonVisiable(true);
            		  //finish();
            	  }
            	  break;
          }
		}
    }
    
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mColorChangeHandler.removeMessages(CHANGE_GREEN);
		mColorChangeHandler.removeMessages(CHANGE_WHITE);
		mColorChangeHandler.removeMessages(CHANGE_BLACK);
		mColorChangeHandler.removeMessages(CHANGE_RED);
		mColorChangeHandler.removeMessages(CHANGE_FINISH);
		if(mWakeLock!=null)
		{
			mWakeLock.release(); 
			mWakeLock = null;
		}
	}	    
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		
		finish();
		super.onStop();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (TestControl.IsAuto()&&!mFinishFlag) {
			return true;
		}
		int result = TestControl.GetKeyEvent(keyCode);
		if (result != TestControl.DO_NOTHING) {
			setResult(result);
			finish();
	    }
	    return true;
	}
}
		 